package tv.huan.tencent.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tv.huan.common.core.text.Convert;
import tv.huan.common.utils.DateUtils;
import tv.huan.tencent.domain.CardBuyLog;
import tv.huan.tencent.domain.CardPool;
import tv.huan.tencent.mapper.CardBuyLogMapper;
import tv.huan.tencent.mapper.CardConsumeLogMapper;
import tv.huan.tencent.domain.CardConsumeLog;
import tv.huan.tencent.mapper.CardPoolMapper;
import tv.huan.tencent.service.ICardConsumeLogService;
import tv.huan.tencent.service.ICardPoolService;

/**
 * 消费记录Service业务层处理
 * 
 * @author admin
 * @date 2020-12-23
 */
@Service
public class CardConsumeLogServiceImpl implements ICardConsumeLogService 
{
    @Autowired
    private CardConsumeLogMapper cardConsumeLogMapper;

    @Autowired
    private CardPoolMapper cardPoolMapper;

    @Autowired
    private CardBuyLogMapper cardBuyLogMapper;

    @Autowired
    private ICardPoolService cardPoolService;

    /**
     * 查询消费记录
     * 
     * @param id 消费记录ID
     * @return 消费记录
     */
    @Override
    public CardConsumeLog selectCardConsumeLogById(Integer id)
    {
        return cardConsumeLogMapper.selectCardConsumeLogById(id);
    }

    /**
     * 查询消费记录列表
     * 
     * @param cardConsumeLog 消费记录
     * @return 消费记录
     */
    @Override
    public List<CardConsumeLog> selectCardConsumeLogList(CardConsumeLog cardConsumeLog)
    {
        return cardConsumeLogMapper.selectCardConsumeLogList(cardConsumeLog);
    }

    /**
     * 新增消费记录
     * 
     * @param cardConsumeLog 消费记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCardConsumeLog(CardConsumeLog cardConsumeLog)
    {
        cardConsume(cardConsumeLog);
        cardConsumeLog.setCreateTime(DateUtils.getNowDate());
        cardConsumeLogMapper.insertCardConsumeLog(cardConsumeLog);
//        cardPoolService.checkCardPoolAndSendEmail();
        return 1;
    }

//    消费卡池逻辑
    private void cardConsume(CardConsumeLog cardConsumeLog){
        //订购数量
        int orderCount = cardConsumeLog.getOrderCount();
        //开通数量
        int openCount = cardConsumeLog.getOpenCount();
        //获取卡池
        CardPool cp = cardPoolMapper.selectCardPoolById(cardConsumeLog.getCardId());
        int totalPredictRemainder = cp.getPredictRemainder();
        int totalActualRemainder = cp.getActualRemainder();
        cp.setPredictRemainder(totalPredictRemainder - orderCount);
        cp.setActualRemainder(totalActualRemainder - openCount);
        cardPoolMapper.updateCardPool(cp);
        //获取有效的购买记录按入库时间正序
        List<CardBuyLog> cardBuyLogList = cardBuyLogMapper.selectCardBuyLogListByCardId(cp.getCardId());
        //当前购买信息预估余量
        int currentPredictRemainder;
        for (CardBuyLog cbl : cardBuyLogList){
            currentPredictRemainder = cbl.getPredictRemainder();
            if (currentPredictRemainder > 0){
                if (currentPredictRemainder >= orderCount){
                    cbl.setPredictRemainder(currentPredictRemainder - orderCount);
                    cardBuyLogMapper.updateCardBuyLog(cbl);
                    break;
                }else {
                    cbl.setPredictRemainder(0);
                    cardBuyLogMapper.updateCardBuyLog(cbl);
                    orderCount = orderCount - currentPredictRemainder;
                }
            }
        }
        //当前购买信息实际余量
        int currentActualRemainder;
        for (CardBuyLog cbl : cardBuyLogList){
            currentActualRemainder = cbl.getActualRemainder();
            if (currentActualRemainder > 0){
                if (currentActualRemainder >= openCount){
                    cbl.setActualRemainder(currentActualRemainder - openCount);
                    cardBuyLogMapper.updateCardBuyLog(cbl);
                    break;
                }else {
                    cbl.setActualRemainder(0);
                    cardBuyLogMapper.updateCardBuyLog(cbl);
                    openCount = openCount - currentActualRemainder;
                }
            }
        }
    }

    /**
     * 修改消费记录
     * 
     * @param cardConsumeLog 消费记录
     * @return 结果
     */
    @Override
    public int updateCardConsumeLog(CardConsumeLog cardConsumeLog)
    {
        cardConsumeLog.setUpdateTime(DateUtils.getNowDate());
        return cardConsumeLogMapper.updateCardConsumeLog(cardConsumeLog);
    }

    /**
     * 删除消费记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCardConsumeLogByIds(String ids)
    {
        return cardConsumeLogMapper.deleteCardConsumeLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除消费记录信息
     * 
     * @param id 消费记录ID
     * @return 结果
     */
    public int deleteCardConsumeLogById(Integer id)
    {
        return cardConsumeLogMapper.deleteCardConsumeLogById(id);
    }
}
